package com.zzw.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzw.springboot.common.Result;

import javax.annotation.Resource;
import java.util.List;

import com.zzw.springboot.service.IDictService;
import com.zzw.springboot.entity.Dict;

import org.springframework.web.bind.annotation.RestController;

/**
 * @Author zzw2000
 * @Date 2022-06-02 08:04:47
 * @Description Dict前端控制器
 */
@RestController
@RequestMapping("/dict")
    public class DictController {
        @Autowired
    private IDictService dictService;

    /**
     * 保存
     * @param dict
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Dict dict){
        return Result.success(dictService.saveOrUpdate(dict));
    }
    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(dictService.removeById(id));
    }
    /**
     * 根据id批量删除dict
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result delete(@RequestBody List<Integer> ids) {
        return Result.success(dictService.removeBatchByIds(ids));
    }
    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public Result findAll() {
        return Result.success(dictService.list());
    }
    /**
     * 查询单条记录
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(dictService.getById(id));
    }
    /**
     * 分页查询
     * @param pageNum 当前页码
     * @param pageSize 每页显示的条数
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize) {
        IPage<Dict> dictPage =dictService.page(new Page<>(pageNum, pageSize));
        return Result.success(dictPage);
        }
}

